
#include <stdio.h>
#include "Nano100Series.h"
#include "GPIO_IIC.h"



#define DELAY_BIT	1		//for iic speed


/*---------------------------------------------------------------------------------------------------------*/
/* Global variables                                                                                        */
/*---------------------------------------------------------------------------------------------------------*/
uint8_t g_u8DeviceAddr;
uint8_t * g_au8TxData;
uint8_t g_u8RxData[5];
volatile uint16_t g_u8DataLen,g_u8data_p;
volatile uint8_t g_u8EndFlag = 0;

typedef void (*I2C_FUNC)(uint32_t u32Status);

static I2C_FUNC s_I2C0HandlerFn = NULL;

/*---------------------------------------------------------------------------------------------------------*/
/*  I2C0 IRQ Handler                                                                                       */
/*---------------------------------------------------------------------------------------------------------*/
void I2C0_IRQHandler(void)
{
    uint32_t u32Status;

    // clear interrupt flag
    I2C0->INTSTS |= I2C_INTSTS_INTSTS_Msk;

    u32Status = I2C_GET_STATUS(I2C0);

    if (I2C_GET_TIMEOUT_FLAG(I2C0)) {
        /* Clear I2C0 Timeout Flag */
        I2C_ClearTimeoutFlag(I2C0);
    } else {
        if (s_I2C0HandlerFn != NULL)
            s_I2C0HandlerFn(u32Status);
    }
}

/*---------------------------------------------------------------------------------------------------------*/
/*  I2C Rx Callback Function                                                                               */
/*---------------------------------------------------------------------------------------------------------*/
void I2C_MasterRx(uint32_t u32Status)
{
	
    if (u32Status == 0x08) {                    /* START has been transmitted and prepare SLA+W */
        I2C_SET_DATA(I2C0, (g_u8DeviceAddr << 1)); /* Write SLA+W to Register I2CDAT */
        I2C_SET_CONTROL_REG(I2C0, I2C_SI);
    } else if (u32Status == 0x18) {             /* SLA+W has been transmitted and ACK has been received */
        I2C_SET_DATA(I2C0, g_au8TxData[0]);
        I2C_SET_CONTROL_REG(I2C0, I2C_SI);
    } else if (u32Status == 0x20) {             /* SLA+W has been transmitted and NACK has been received */
        I2C_SET_CONTROL_REG(I2C0, I2C_STA | I2C_STO | I2C_SI);
    } else if (u32Status == 0x28) {             /* DATA has been transmitted and ACK has been received */
        if (g_u8data_p != 1) {
			g_u8data_p=1;
            I2C_SET_DATA(I2C0, g_au8TxData[1]);
            I2C_SET_CONTROL_REG(I2C0, I2C_SI);
        } else {
        	g_u8data_p=0;
            I2C_SET_CONTROL_REG(I2C0, I2C_STA | I2C_SI);
        }
    } else if (u32Status == 0x10) {             /* Repeat START has been transmitted and prepare SLA+R */
        I2C_SET_DATA(I2C0, (g_u8DeviceAddr << 1) | 0x01);  /* Write SLA+R to Register I2CDAT */
        I2C_SET_CONTROL_REG(I2C0, I2C_SI);
    } else if (u32Status == 0x40) {             /* SLA+R has been transmitted and ACK has been received */
        I2C_SET_CONTROL_REG(I2C0, I2C_SI);
    } else if (u32Status == 0x58) {             /* DATA has been received and NACK has been returned */
    	//if(g_u8DataLen>g_u8data_p)
    		//{
		        g_u8RxData[0] = I2C_GET_DATA(I2C0);
		        I2C_SET_CONTROL_REG(I2C0, I2C_STO | I2C_SI);
		        g_u8EndFlag = 1;
    		//}
		//else
			//{
			//	g_u8RxData[g_u8data_p++] = I2C_GET_DATA(I2C0);
		     //   I2C_SET_CONTROL_REG(I2C0, I2C_SI);
		       
			//}
    } else {
        /* TO DO */
        //printf("Status 0x%x is NOT processed\n", u32Status);
    }
}

/*---------------------------------------------------------------------------------------------------------*/
/*  I2C Tx Callback Function                                                                               */
/*---------------------------------------------------------------------------------------------------------*/
void I2C_MasterTx(uint32_t u32Status)
{
    if (u32Status == 0x08) {                    /* START has been transmitted */
        I2C_SET_DATA(I2C0, g_u8DeviceAddr << 1);  /* Write SLA+W to Register I2CDAT */
        I2C_SET_CONTROL_REG(I2C0, I2C_SI);
    } else if (u32Status == 0x18) {             /* SLA+W has been transmitted and ACK has been received */
        I2C_SET_DATA(I2C0, g_au8TxData[g_u8data_p++]);
        I2C_SET_CONTROL_REG(I2C0, I2C_SI);
    } else if (u32Status == 0x20) {             /* SLA+W has been transmitted and NACK has been received */
        I2C_SET_CONTROL_REG(I2C0, I2C_STA | I2C_STO | I2C_SI);
    } else if (u32Status == 0x28) {             /* DATA has been transmitted and ACK has been received */
        if (g_u8DataLen > g_u8data_p) {
            I2C_SET_DATA(I2C0, g_au8TxData[g_u8data_p++]);
            I2C_SET_CONTROL_REG(I2C0, I2C_SI);
        } else {
            I2C_SET_CONTROL_REG(I2C0, I2C_STO | I2C_SI);
            g_u8EndFlag = 1;
        }
    } else {
        /* TO DO */
       // printf("Status 0x%x is NOT processed\n", u32Status);
    }
}



/********************************************************************/

void i2c_init(void)
{
	 /* Open I2C0 and set clock to 100k */
    I2C_Open(I2C0, 100000);

    

    /* Set I2C0 2 Slave Addresses */
    //I2C_SetSlaveAddr(I2C0, 0, 0x15, I2C_GCMODE_DISABLE);   /* Slave Address : 0x15 */
    //I2C_SetSlaveAddr(I2C0, 1, 0x35, I2C_GCMODE_DISABLE);   /* Slave Address : 0x35 */

    /* Enable I2C0 interrupt */
    I2C_EnableInt(I2C0);
    NVIC_EnableIRQ(I2C0_IRQn);

}

/********************************************************************/

uint8_t i2c_write(uint8_t slave_address, uint8_t *buffer, int byte_count)
{
	uint16_t count=0;
	g_u8DeviceAddr = slave_address;
	g_au8TxData=buffer;
	g_u8DataLen=byte_count;
	g_u8data_p=0;
	g_u8EndFlag = 0;
	/* I2C function to write data to slave */
    s_I2C0HandlerFn = (I2C_FUNC)I2C_MasterTx;

    /* I2C as master sends START signal */
    I2C_SET_CONTROL_REG(I2C0, I2C_STA);

	/* Wait I2C Tx Finish */
    while (g_u8EndFlag == 0&&count++ <20000)
		CLK_SysTickDelay(30);
	//delay(1);

}

/********************************************************************/

uint8_t i2c_read(uint8_t slave_address, uint16_t r_addr,uint8_t *buffer, int byte_count)
{
	uint8_t count=0;
	static uint8_t address[2];
	address[0]=(uint8_t)(r_addr>>8)&0xff;
	address[1]=(uint8_t)(r_addr)&0xff;
	g_u8DeviceAddr = slave_address;
	g_au8TxData=address;
	g_u8DataLen=byte_count;
	g_u8data_p=0;
	g_u8EndFlag = 0;
	
	/* I2C function to write data to slave */
	s_I2C0HandlerFn = (I2C_FUNC)I2C_MasterRx;

	/* I2C as master sends START signal */
	I2C_SET_CONTROL_REG(I2C0, I2C_STA);

	/* Wait I2C Tx Finish */
	while (g_u8EndFlag == 0&&count++<200)
		CLK_SysTickDelay(30);
	buffer[0]=g_u8RxData[0];
	buffer[1]=g_u8RxData[1];
	
}

/********************************************************************/

//void i2c_start(void)
//{	
//	
//	SDA=1;
//	delay(1);

//	SCK=1;
//	delay(1);	
//	  
//	SDA=0;;
//	delay(1);
//	     
//	SCK=0;
//	delay(1);
//}

/********************************************************************/

//uint8_t i2c_stop(void)
//{
//		  
//	SCK=0;
//	delay(1);

//	SDA=0;
//	delay(1);
//	    
//	SCK=1;
//	      
//	delay(1);
//	    
//	SDA=1;	
//	return(1);
//}

/********************************************************************/

void delay(int value)
{
	CLK_SysTickDelay(value);
}

/********************************************************************/


void write_reg_32(uint8_t slaveaddress,uint16_t address,uint32_t data)
{
	uint8_t buf[6];
	buf[0]=address>>8&0xff;
	buf[1]=address&0xff;
	buf[2]=data&0xff;
	buf[3]=data>>8&0xff;
	buf[4]=data>>16&0xff;
	buf[5]=data>>24&0xff;
	i2c_write(slaveaddress, buf,6);
}

void write_reg_16(uint8_t slaveaddress,uint8_t address,uint16_t data)
{
	uint8_t buf[4];
	//buf[0]=address>>8&0xff;
	buf[0]=address&0xff;
	buf[1]=data&0xff;
	buf[2]=data>>8&0xff;
	//buf[4]=data>>16&0xff;
	//buf[5]=data>>24&0xff;
	i2c_write(slaveaddress, buf,3);
}

//uint16_t read_reg_16(uint8_t slaveaddress,uint8_t address)
//{
//	uint8_t buf[4];
//	//buf[0]=address>>8&0xff;
//	buf[0]=address&0xff;
//	
//	//buf[4]=data>>16&0xff;
//	//buf[5]=data>>24&0xff;
//	i2c_write(slaveaddress, buf,1);
//	i2c_read(slaveaddress, buf,2);
//	return buf[0]<<8 |buf[1];
//}

void write_reg_8(uint8_t slaveaddress,uint8_t address,uint8_t data)
{
	uint8_t buf[2];
	buf[0]=address&0xff;
	buf[1]=data;
	//buf[2]=data&0xff;
	//buf[3]=data>>8&0xff;
	//buf[4]=data>>16&0xff;
	//buf[5]=data>>24&0xff;
	i2c_write(slaveaddress, buf,2);
}


#define SDA_1	PA1
#define SCK_1	PA0
#define SDA_2	PA3
#define SCK_2	PA2

#define SDA_3	PC1
#define SCK_3	PC0


/********************************************************************/

void LVDS_i2c_init(void)
{
//	GPIO_SetMode(PC, BIT5, GPIO_PMD_OUTPUT);
//	GPIO_SetMode(PC, BIT6, GPIO_PMD_OUTPUT);
	GPIO_SetMode(PA, BIT2, GPIO_PMD_OUTPUT);
	GPIO_SetMode(PA, BIT3, GPIO_PMD_OUTPUT);
	GPIO_SetMode(PA, BIT0, GPIO_PMD_OUTPUT);
	GPIO_SetMode(PA, BIT1, GPIO_PMD_OUTPUT);
	SDA_1=1;
	SCK_1=1;
	SDA_2=1;
	SCK_2=1;
	SDA_3=1;
	SCK_3=1;

}

/********************************************************************/

uint8_t LVDS_i2c_write(uint8_t ch,uint8_t slave_address, uint8_t *buffer, int byte_count)
{
	uint8_t out_mask = 0x80;
	uint8_t value = 0x00;
	uint8_t send_byte = 0x00;
	uint8_t status = IIC_ERROR;
	int count = 8;	
	int i = 0;
	
	slave_address = (slave_address & 0xFE);
         
	LVDS_i2c_start(ch);
	//delay(500);

	send_byte = slave_address;
        
	for(i = 0; i <= byte_count; i++)
	{    
	count = 8;  
	out_mask = 0x80;
	     
	/* Send data bytes one bit at a time. */   
	while(count > 0)
	{            
	 value = ((send_byte & out_mask) ? 1 : 0);
	  if (value == 1)
	  {
	  	if(ch==1) SDA_1=1; else if(ch==2) SDA_2=1; else if(ch==3) SDA_3=1;
	  }
	  else
	  {
	   	if(ch==1) SDA_1=0; else if(ch==2) SDA_2=0; else if(ch==3) SDA_3=0;
	  }
	   
	  delay(DELAY_BIT);     
	               
	  if(ch==1) SCK_1=1; else if(ch==2) SCK_2=1; else if(ch==3)	 SCK_3=1;

	  delay(DELAY_BIT);  
	   
	  if(ch==1) SCK_1=0; else if(ch==2) SCK_2=0; else if(ch==3) SCK_3=0;
	  delay(DELAY_BIT); 
	   
	  out_mask >>= 1; 
	  count--;   
	 }
	 
	 if(ch==1) SDA_1=1; else if(ch==2) SDA_2=1; else if(ch==3) SDA_3=1 ; /* Let go of data pin. */
	 delay(DELAY_BIT); 

	if(ch==1)
		GPIO_SetMode(PA, BIT1, GPIO_PMD_INPUT);
	else if(ch==2)
		GPIO_SetMode(PA, BIT3, GPIO_PMD_INPUT);
	else if(ch==3)
		GPIO_SetMode(PA, BIT3, GPIO_PMD_INPUT);
	delay(DELAY_BIT); 
	 if(ch==1)
	 	{
	 		if(SDA_1==1)
	 			{
	 				status = IIC_NO_ACK; /* Transfer complete, bus busy, acknowledge not received. */
	  				break;
	 			}
			
	 	}
	 else if(ch==2)
	 	{
	 		if(SDA_2==1)
	 			{
	 				status = IIC_NO_ACK; /* Transfer complete, bus busy, acknowledge not received. */
	  				break;
	 			}
	 	}
	 else if(ch==3)
	 	{
	 		if(SDA_3==1)
	 			{
	 				status = IIC_NO_ACK; /* Transfer complete, bus busy, acknowledge not received. */
	  				break;
	 			}
	 	}
	 	
	 if(ch==1)
		GPIO_SetMode(PA, BIT1, GPIO_PMD_OUTPUT);
	else if(ch==2)
		GPIO_SetMode(PA, BIT3, GPIO_PMD_OUTPUT);
	else if(ch==3)
		GPIO_SetMode(PA, BIT3, GPIO_PMD_OUTPUT);
	  
	// if (((ch==1)? SDA_1 : SDA_2 )== 1)
	 //{ 
	 // status = IIC_NO_ACK; /* Transfer complete, bus busy, acknowledge not received. */
	 // break; 
	 //} /* If not acknowledged, exit loop. */
	 
	 if(ch==1) SCK_1=1; else if(ch==2) SCK_2=1; else if(ch==3) SCK_3=1;
	 delay(DELAY_BIT);  

	 if(ch==1) SCK_1=0; else if(ch==2) SCK_2=0; else if(ch==3) SCK_3=0;
	 status = IIC_COMPLETE;  /* Transfer complete, bus busy, acknowledge received. */
	 delay(DELAY_BIT); 
	 
	 send_byte = buffer[i];      
	}

	if(ch==1) SDA_1=1; else if(ch==2) SDA_2=1; else if(ch==3) SDA_3=1;
	if(ch==1) SCK_1=1; else if(ch==2) SCK_2=1; else if(ch==3) SCK_3=1;	
	return(status);
}

/********************************************************************/

uint8_t LVDS_i2c_read(uint8_t ch,uint8_t slave_address, uint8_t *buffer, int byte_count)
{
uint8_t input_byte = 0x00;
uint8_t value = 0x00;
uint8_t out_mask = 0x80;
uint8_t status = IIC_ERROR;
int count = 8;
int i = 0;



slave_address = (slave_address | 0x01);
   
LVDS_i2c_start(ch);
//delay(500);
               
/**********  Write Address Procedure **********/
  
   while(count > 0)
   {
    value = ((slave_address & out_mask) ? 1 : 0);
     if (value == 1)
     {
      if(ch==1) SDA_1=1; else if(ch==2) SDA_2=1; else if(ch==3) SDA_3=1;
	 }
     else
     {
      if(ch==1) SDA_1=0; else if(ch==2) SDA_2=0; else if(ch==3) SDA_3=0;
	 }
      delay(DELAY_BIT);      
                   
     if(ch==1) SCK_1=1; else if(ch==2) SCK_2=1; else if(ch==3) SCK_3=1;
      
     delay(DELAY_BIT); 
       
     if(ch==1) SCK_1=0; else if(ch==2) SCK_2=0; else if(ch==3) SCK_3=0;
     delay(DELAY_BIT);         
     out_mask >>= 1; 
     count--;         
  }
     
    if(ch==1) SDA_1=1; else if(ch==2) SDA_2=1; else if(ch==3) SDA_3=1;  /* Let go of data pin. */
    delay(DELAY_BIT);
    if(ch==1) SCK_1=1; else if(ch==2) SCK_2=1; else if(ch==3) SCK_3=1;
    delay(DELAY_BIT);  
    if(ch==1)
		GPIO_SetMode(PA, BIT1, GPIO_PMD_INPUT);
	else if (ch==2)
		GPIO_SetMode(PA, BIT3, GPIO_PMD_INPUT);
	else if (ch==3)
		GPIO_SetMode(PA, BIT3, GPIO_PMD_INPUT);
    /* If not acknowleged, set status accordingly and exit read process. */
   if(ch==1)
	 	{
	 		if(SDA_1==1)
	 			{
	 				status = IIC_NO_ACK; /* Transfer complete, bus busy, acknowledge not received. */
	  				return(0);
	 			}
			
	 	}
	 else if(ch==2)
	 	{
	 		if(SDA_2==1)
	 			{
	 				status = IIC_NO_ACK; /* Transfer complete, bus busy, acknowledge not received. */
	  				return(IIC_NO_ACK);
	 			}
	 	}
	 else if(ch==3)
	 	{
	 		if(SDA_3==1)
	 			{
	 				status = IIC_NO_ACK; /* Transfer complete, bus busy, acknowledge not received. */
	  				return(IIC_NO_ACK);
	 			}
	 	}
    if(ch==1)
		GPIO_SetMode(PA, BIT1, GPIO_PMD_OUTPUT);
	else if(ch==2)
		GPIO_SetMode(PA, BIT3, GPIO_PMD_OUTPUT);
	else if(ch==3)
		GPIO_SetMode(PA, BIT3, GPIO_PMD_OUTPUT);
    if(ch==1) SCK_1=0; else if(ch==2) SCK_2=0; else if(ch==3) SCK_3=0;
    delay(DELAY_BIT);

	/**********  Begin Read Procedure **********/

	/* Release ((ch==1)? SDA_1 : SDA_2 ) and SCL to initiate transfer. */
	if(ch==1) SDA_1=1; else if(ch==2) SDA_2=1; else if(ch==3) SDA_3=1;
	if(ch==1) SCK_1=1; else if(ch==2) SCK_2=1; else if(ch==3) SCK_3=1;
	if(ch==1)
		GPIO_SetMode(PA, BIT1, GPIO_PMD_INPUT);
	else if(ch==2)
		GPIO_SetMode(PA, BIT3, GPIO_PMD_INPUT);
	else if(ch==3)
		GPIO_SetMode(PA, BIT3, GPIO_PMD_INPUT);

	for(i = 0; i < byte_count; i++)
	{
	  /* Release ((ch==1)? SDA_1 : SDA_2 ) and SCL to initiate transfer. */
	  if(ch==1) SDA_1=1; else if(ch==2) SDA_2=1; else if(ch==3) SDA_3=1;
	  if(ch==1) SCK_1=1; else if(ch==2) SCK_2=1; else if(ch==3) SCK_3=1;
	  if(ch==1)
		GPIO_SetMode(PA, BIT1, GPIO_PMD_INPUT);
	  else if(ch==2)
		GPIO_SetMode(PA, BIT3, GPIO_PMD_INPUT);
	  else if(ch==3)
		GPIO_SetMode(PA, BIT3, GPIO_PMD_INPUT);
	  count = 8;
	  input_byte = 0x00;
	          
	  /* Loop for bit-by-bit read of data. */
	  while(count > 0) 
		  {
			if(ch==1) SCK_1=1; else if(ch==2) SCK_2=1; else if(ch==3) SCK_3=1;
			delay(DELAY_BIT);  
			delay(4); /* Required to make read and write clocks the same freq. */

//			if (((ch==1)? SDA_1 : SDA_2 )==1)
//				input_byte++;
			if(ch==1 && SDA_1==1)
				input_byte++;
			else if(ch==2 && SDA_2==1)
				input_byte++;
			else if(ch==3 && SDA_3==1)
				input_byte++;

			delay(DELAY_BIT);  

			if(ch==1) SCK_1=0; else if(ch==2) SCK_2=0; else if(ch==3) SCK_3=0;
			delay(DELAY_BIT); 
			  
			if (count == 1)
				break;
			else  
				input_byte <<= 1;

			count--;
		  }

	  /* Write input byte to "read_buffer". */
	  buffer[i] = input_byte;
	    
	  if(i == (byte_count - 1))
	   break;
	     
	     /* Below is the acknowledge procedure. */     
	     if(ch==1)
			GPIO_SetMode(PA, BIT1, GPIO_PMD_OUTPUT);
		else if(ch==2)
			GPIO_SetMode(PA, BIT3, GPIO_PMD_OUTPUT);
		else if(ch==3)
			GPIO_SetMode(PA, BIT3, GPIO_PMD_OUTPUT);
		
	     if(ch==1) SDA_1=0; else if(ch==2) SDA_2=0; else if(ch==3) SDA_3=0;
	     delay(DELAY_BIT);  
	     if(ch==1) SCK_1=1; else if(ch==2) SCK_2=1; else if(ch==3) SCK_3=1;    
	     delay(DELAY_BIT);  
	    
	     if(ch==1) SCK_1=0; else if(ch==2) SCK_2=0; else if(ch==3) SCK_3=0;
	     delay(DELAY_BIT);  
	     if(ch==1) SDA_1=1; else if(ch==2) SDA_2=1; else if(ch==3) SDA_3=1;
	     status = IIC_COMPLETE; 
	}     

/* Standard protocol calls for the last read byte to
    not receive an acknowledge from the master. */  
     if(ch==1)
		GPIO_SetMode(PA, BIT1, GPIO_PMD_OUTPUT);
	else if(ch==2)
		GPIO_SetMode(PA, BIT3, GPIO_PMD_OUTPUT);
	else if(ch==3)
		GPIO_SetMode(PA, BIT3, GPIO_PMD_OUTPUT);
	if(ch==1) SDA_1=1; else if(ch==2) SDA_2=1; else if(ch==3) SDA_3=1;
	if(ch==1) SCK_1=1; else if(ch==2) SCK_2=1; else if(ch==3) SCK_3=1;     
	delay(DELAY_BIT); 
	 
	if(ch==1) SCK_1=0; else if(ch==2) SCK_2=0; else if(ch==3) SCK_3=0;
	delay(DELAY_BIT);  
	 if(ch==1) SDA_1=1; else if(ch==2) SDA_2=1; else if(ch==3) SDA_3=1;
	return(status);
}

/********************************************************************/

void LVDS_i2c_start(uint8_t ch)
{	
	GPIO_SetMode(PA, BIT0, GPIO_PMD_OUTPUT);
	GPIO_SetMode(PA, BIT1, GPIO_PMD_OUTPUT);
	GPIO_SetMode(PA, BIT2, GPIO_PMD_OUTPUT);
	GPIO_SetMode(PA, BIT3, GPIO_PMD_OUTPUT);
	//GPIO_SetMode(PA, BIT14, GPIO_PMD_OUTPUT);
	//GPIO_SetMode(PA, BIT15, GPIO_PMD_OUTPUT);
	
	 if(ch==1) SDA_1=1; else if(ch==2) SDA_2=1; else if(ch==3) SDA_3=1;
	//delay(100);

	if(ch==1) SCK_1=1; else if(ch==2) SCK_2=1; else if(ch==3) SCK_3=1;  
	//delay(100);	
	  
	 if(ch==1) SDA_1=0; else if(ch==2) SDA_2=0; else if(ch==3) SDA_3=0;
	//delay(100);
	     
	if(ch==1) SCK_1=0; else if(ch==2) SCK_2=0; else if(ch==3) SCK_3=0;  
	delay(100);
}

/********************************************************************/

uint8_t LVDS_i2c_stop(uint8_t ch)
{
		  
	if(ch==1) SCK_1=0; else if(ch==2) SCK_2=0; else if(ch==3) SCK_3=0;  
	//delay(100);

	if(ch==1) SDA_1=0; else if(ch==2) SDA_2=0; else if(ch==3) SDA_3=0;
	//delay(100);
	    
	if(ch==1) SCK_1=1; else if(ch==2) SCK_2=1; else if(ch==3) SCK_3=1;  
	      
	
	    
	if(ch==1) SDA_1=1; else if(ch==2) SDA_2=1; else if(ch==3) SDA_3=1;	
	delay(100);
	return(1);
}


void LVDS_write_reg_32(uint8_t ch,uint8_t slaveaddress,uint16_t address,uint32_t data)
{
	uint8_t buf[6];
	buf[0]=address>>8&0xff;
	buf[1]=address&0xff;
	buf[2]=data&0xff;
	buf[3]=data>>8&0xff;
	buf[4]=data>>16&0xff;
	buf[5]=data>>24&0xff;
	LVDS_i2c_write(ch,slaveaddress, buf,6);
}

void LVDS_write_reg_8(uint8_t ch,uint8_t slaveaddress,uint8_t address,uint8_t data)
{
	uint8_t buf[6];
	buf[0]=address;
	buf[1]=data;
	
	LVDS_i2c_write(ch,slaveaddress, buf,2);
}



 

